<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="50%"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="学院" prop="collegeId">
            <college-selector v-model="form.collegeId"></college-selector>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="工号" prop="jobNumber">
            <el-input v-model="form.jobNumber" placeholder="请输入工号" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="教师姓名" prop="teacherName">
            <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号码" prop="identityCardNumber">
            <el-input
              v-model="form.identityCardNumber"
              placeholder="请输入身份证号码"
              @change="getAgeSexInfo"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="教师性别" prop="teacherSex">
            <el-select
              v-model="form.teacherSex"
              placeholder="请选择性别"
              clearable
              size="small"
              disabled
            >
              <el-option
                v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄" prop="teacherAge">
            <el-input
              type="number"
              v-model="form.teacherAge"
              placeholder="请输入教师年龄"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="出生日期" prop="teacherBirthday">
            <el-date-picker
              clearable
              v-model="form.teacherBirthday"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择出生日期"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="teacherTelephone">
            <el-input
              v-model="form.teacherTelephone"
              placeholder="请输入联系电话"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="法名" prop="legalName">
            <el-input v-model="form.legalName" placeholder="请输入法名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="微信号">
            <el-input v-model="form.wechat" placeholder="请输入微信号" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="民族">
            <el-input v-model="form.nation" placeholder="请输入民族" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="籍贯">
            <el-input v-model="form.natively" placeholder="请输入籍贯所在地" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="职称" prop="post">
            <el-select
              v-model="form.post"
              placeholder="请选择职称（教授、副教授）"
            >
              <el-option
                v-for="dict in this.getDictDatas(
                  DICT_TYPE.TEACHER_PROFESSOR_TYPE
                )"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="研究方向">
            <el-input
              v-model="form.researchDirection"
              placeholder="请输入研究方向"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="聘用时间" prop="employTime">
            <el-date-picker
              clearable
              v-model="form.employTime"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择聘用时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用类型" prop="employType">
            <el-select v-model="form.employType" placeholder="请选择聘用类型">
              <el-option
                v-for="dict in this.getDictDatas(
                  DICT_TYPE.TEACHER_RECRUIT_TYPE
                )"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="教师简介" prop="remark">
            <el-input
              type="textarea"
              :rows="2"
              v-model="form.teacherIntroduce"
              placeholder="请输入教师简介"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              :rows="2"
              v-model="form.remark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- <el-form-item label="编号" prop="teacherNumber">
        <el-input v-model="form.teacherNumber" placeholder="请输入编号" />
      </el-form-item> -->

      <!--
            <el-form-item label="修行类型" prop="practiceType">
              <el-select v-model="form.practiceType" placeholder="请选择修行类型">
                <el-option
                  v-for="dict in this.getDictDatas(DICT_TYPE.STUDENT_MONASTICAL_TYPE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item> -->
      <!-- <el-form-item label="院校id" prop="collageId">
        <el-input v-model="form.collageId" placeholder="请输入院校id" />
      </el-form-item> -->
      <!-- <el-form-item label="机构编号" prop="orgId">
        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  createTeacherTeachers,
  updateTeacherTeachers,
} from "@/api/collegebase/teacherTeachers";
import common from "@/api/collegebase/common";
import CollegeSelector from "@/components/CollegeSelector";

export default {
  name: "TeacherTeachersEdit",
  components: { CollegeSelector },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegeId: [
          { required: true, message: '请选择学院', trigger: 'blur' },
        ],
        identityCardNumber: [
          { required: true, message: '请输入身份证号码', trigger: 'blur' },
        ],
        teacherName: [
          { required: true, message: '请输入教师姓名', trigger: 'blur' },
        ],
        teacherSex: [
          { required: true, message: '请选择教师性别', trigger: 'blur' },
        ],
        teacherTelephone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
        ],
        post: [
          { required: true, message: '请选择职称', trigger: 'blur' },
        ],
        employType: [
          { required: true, message: '请选择聘用类型', trigger: 'blur' },
        ],
        jobNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' },
        ],

      },
      //学院
      oldCollegeId: null
    };
  },
  created() {
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {


    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.oldCollegeId = rowData.collegeId;
        // this.form.collegeId = rowData.collegeId;
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        jobNumber: undefined,
        teacherNumber: undefined,
        legalName: undefined,
        teacherName: undefined,
        teacherSex: undefined,
        teacherAge: undefined,
        teacherBirthday: undefined,
        practiceType: undefined,
        collegeId: undefined,
        post: undefined,
        employType: undefined,
        nation: undefined,
        natively: undefined,
        researchDirection: undefined,
        wechat: undefined,
        employTime: undefined,
        teacherTelephone: undefined,
        orgId: undefined,
        teacherIntroduce: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          if (this.oldCollegeId != this.form.collegeId) {
            this.$modal.confirm('修改学院会删除原有学院绑定的班级，是否继续！').then(() => {
              updateTeacherTeachers(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.cancel();
                this.$emit("success");
              });
            }).catch(() => {
            });
          } else {
            updateTeacherTeachers(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.cancel();
              this.$emit("success");
            });
          }
          return;
        }
        // 添加的提交
        createTeacherTeachers(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },

    //根据身份证获取年龄
    getAgeSexInfo(idCard) {
      console.log(idCard);
      let sex = null;
      let birth = null;
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;

      if (idCard.length === 18) {
        age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
        sex = idCard.substring(16, 17);
        birth = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
        if (idCard.substring(10, 12) < month || idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day)
          age++;

      }
      if (idCard.length === 15) {
        age = myDate.getFullYear() - idCard.substring(6, 8) - 1901;
        sex = idCard.substring(13, 14);
        birth = "19" + idCard.substring(6, 8) + "-" + idCard.substring(8, 10) + "-" + idCard.substring(10, 12);
        if (idCard.substring(8, 10) < month || idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day)
          age++;
      }

      if (sex % 2 === 0)
        sex = 2; // 性别代码 1代表男，2代表女，暂时不涉及其他类型性别
      else
        sex = 1;
      this.$set(this.form, "teacherSex", sex + "");
      this.$set(this.form, "teacherAge", age);
      this.$set(this.form, "teacherBirthday", birth);
    }
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}
</style>
